BasinCumulate Subroutine

private subroutine BasinCumulate(var, conv)

cumulate variable along stream network

Arguments

Type IntentOptional Attributes Name
type(grid_real), intent(in) :: var
real(kind=float), intent(in), optional :: conv

conversion factor


Variables

Type Visibility Attributes Name Initial
real(kind=float), public :: factor
integer(kind=short), public :: i
integer(kind=short), public :: is
integer(kind=short), public :: j
integer(kind=short), public :: js
integer(kind=short), public :: k

Source Code

SUBROUTINE BasinCumulate   & 
!
 ( var, conv )  

IMPLICIT NONE

!arguments with intent in:    
TYPE (grid_real), INTENT (IN) :: var
REAL (KIND = float), OPTIONAL, INTENT (IN) :: conv !!conversion factor

!local declaration:
INTEGER (KIND = short) :: i, j, k, is, js
REAL (KIND = float) :: factor

!-----------------------------end of declarations------------------------------

!reset cumulated grid
cum = 0.

!cobversion factor
IF ( PRESENT (conv) ) THEN
    factor = conv
ELSE
    factor = 1.
END IF

DO k = 1, streamNetwork % nreach
    
  i = streamNetwork % branch (k) % i0
  j = streamNetwork % branch (k) % j0
  
  
  !follow the branch
  DO WHILE ( .NOT.( j == streamNetwork % branch (k) % j1 .AND. &
                    i == streamNetwork % branch (k) % i1  )    )
 
      
    !find downstream cell
    CALL DownstreamCell (i, j, flowDirection % mat(i,j), is, js )
   
    !if current cell is basin outlet exit
    IF ( CheckOutlet (i, j, is, js, flowDirection) ) THEN
        CYCLE
    END IF
    

    !add current cell
    cum % mat (i,j) = cum % mat (i,j) + var % mat (i,j) * factor
    
    !cumulate downstream
    cum % mat (is, js) = cum % mat (is, js) + cum % mat (i, j)
    
    !select downstream cell for next loop
    j = js
    i = is
  END DO  
END DO


!divide by number of cells in the basin to compute the mean
DO j = 1, cum % jdim
    DO i = 1, cum % idim
        IF ( cum % mat (i,j) /= cum % nodata ) THEN
            cum % mat (i,j) = cum % mat (i,j) / flowAccumulation % mat (i,j)
        END IF
    END DO
END DO

RETURN

 END SUBROUTINE BasinCumulate